var api_upload_host = $('input[name=api_upload_host]').val()
console.log('api_upload_host=' + api_upload_host);
var uploader = new plupload.Uploader({
    runtimes : 'html5,flash,silverlight,html4',
    browse_button : ["add-middle-btn", "add-carousel-btn"],
    flash_swf_url : '/static/js/plupload/Moxie.swf',
    silverlight_xap_url : '/static/js/plupload/Moxie.xap',
    //url : 'http://test.api.ndmooc.com/v1/file/upload?token=' + token,
    url : api_upload_host,
    filters: {
        mime_types : [
            { title : "Image files", extensions : "png,jpg,jpeg,gif,bmp"},
        ],
        max_file_size : '6mb', //最大只能上传10mb的文件
        prevent_duplicates : true //不允许选取重复文件
    },
    // multipart_params: {"token": "ea6f564ab505b95c398cc0c061b4549b0a66ad03"},
    init: {
        PostInit: function() {
            //$('#' + show_id).html('');
        },

        FilesAdded: function(up, files) {
//
//            plupload.each(files, function(file) {
//                //document.getElementById(show_id).innerHTML +='<div class="up-progress" id="' + file.id + '"><p class="up-progress-p"></p></div>'
//            });
            up.start();
            return false;
        },

        BeforeUpload: function(up, file) {
        },

        UploadProgress: function(up, file) {
            //$('#' + file.id).find('p').css({'width': file.percent+'%'})
            // setTimeout(function(){$('#' + file.id).remove(), 2000});
        },

        FileUploaded: function(up, file, info) {
            console.log('up=' + up);
            var info = $.parseJSON(info.response)
            if (info['errcode'] == 0){
                // $("#upload_list").empty()
                img_url = info['data']['url']
                // var img_ = '<img style="display: block;" class="fileImg" src="'+ img_url +'" data-holder-rendered="true">';
                // $("#upload_list").append(img_);
                $("#imghead").attr('src', img_url)
                $("#title-avatars").val(img_url)
                $('.imgLogo').css({"z-index":'5'})
            }
            console.info(info)


            // 上传轮播图
            /*try {
                save_image(info.file_url, file.type, file.size)
            } catch (error) {
                console.log('not carousel figure');
            }*/
        },

        UploadComplete: function(up, files){
            files.shift()
        },

        Error: function(up, err) {
            if (err.code == -600) {
                window.wxc.xcConfirm("选择的文件太大了!", window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
                    window.location.reload();
                }});
            }else if (err.code == -601) {
                window.wxc.xcConfirm("选择的文件后缀不对!", window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
                    window.location.reload();
                }});
            }else if (err.code == -602) {
                window.wxc.xcConfirm("这个文件已经在上传列表中!", window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
                    window.location.reload();
                }});
            }else{
                window.wxc.xcConfirm('err-code:' + err.code +';'+  err.response, window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
                    window.location.reload();
                }});
            }
        }
    }
});
uploader.init();